
--通天塔功能
--#include "data\config\fuben\SkyTowerConfig.lua" once

SKYTOWER_STAGE_FLOOR_NUM		= 5				--通天塔每阶楼层数量

SKYTOWER_FUBEN_FLOORIDX			= 1				--当前通天塔层级
SKYTOWER_FUBEN_BOSS_BEGINTIME	= 2				--当前BOSS的开启时间
SKYTOWER_FUBEN_CHANLLEGE_TYPE	= 3				--挑战类型，1-正常挑战，2-再次挑战

SkyTowerRankColumn = 
{	
	{"name"},				--0，玩家Name
	{"serverId"},			--1，玩家ServerId(留空)
	{"job"},				--2，玩家职业
	{"topFloor"},			--3，玩家通关最高楼层
	{"passTime"},			--4，玩家通关最高楼层的时间
}

SkyTowerRank = Lang.Rank.SkyTowerRank			--通天塔排行榜榜

function GmDoSkyTower( sysarg, flag, param )
	print("GmDoSkyTower, flag="..flag..", param="..param)
	if flag == 1 then
		ChanllegeSkyTower( sysarg, true )
	elseif flag == 2 then
		ReChanllegeSkyTower( sysarg )
	elseif flag == 3 then
	elseif flag == 4 then
		local sVar = Actor.getStaticVar(sysarg)
		sVar.skyTowerData = nil
	elseif flag == 5 then
		OnSkyTowerDailyProc( sysarg )
	end
end


--[[
注册一些事件
]]
function ResisterSkyTowerProc()
	--print("ResisterSkyTowerProc...")
	SetNeedReloginFuben( SkyTowerCfg.fubenId, GlobalLuaConfig.reloginFuben.skyTower, GetElemsForReLoginSkyTower )
	SceneLogoutDispatcher.register( SkyTowerCfg.sceneId, OnCommonFubenLogout )					--注册场景、副本中下线触发的事件
	FubenTimeoutDispatcher.register( SkyTowerCfg.fubenId, OnSingleCommonFubenTimeout )			--注册单人场景、副本时间到后触发的事件
	SceneDeathDispatcher.register( SkyTowerCfg.sceneId, OnSkyTowerActorDeath )					--注册场景、副本中角色死亡触发的事件（非击杀）
	SceneExitDispatcher.register( SkyTowerCfg.sceneId,  OnCommonPersonSceneExit )				--注册离开场景、副本后触发的事件
	--SceneHandExitDispatcher.register( SkyTowerCfg.sceneId,  OnCommonSceneHandExit )			--注册离开场景、副本后触发的事件
end

--[[
初始化周榜
]]
function InitSkyTowerRank()
	--print("*****>>InitSkyTowerRank...")
	Rank.Init(SkyTowerRank, SkyTowerRank, SkyTowerRankColumn, 2000, 0)
end

--[[
掉线恢复的一些配置
]]
function GetElemsForReLoginSkyTower( fubenPtr )
	local fubenCfg = {}
	fubenCfg.fubenId 			= SkyTowerCfg.fubenId
	fubenCfg.enterPos 			= SkyTowerCfg.enterPos
	local InitFubenRightPanel 	= InitSkyTowerPanel 			--数据从fubenPtr中获取
	local loginLog   			= "skyTower login" 
	local loginLogOk			= "skyTower login ok" 
	return fubenCfg, InitFubenRightPanel, loginLog, loginLogOk
end

--[[

]]
function OnSkyTowerActorDeath( sysarg )
	local sceneId 	= Actor.getSceneId( sysarg )
	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	local fubenId 	= Fuben.getFuBenId(fubenPtr)
	--print("OnSkyTowerActorDeath, sceneId="..sceneId..", fubenId="..fubenId)
	OnSkyTowerOver( sysarg, fubenPtr, 0 )
	Actor.exitFuben( sysarg )
end


function OnSkyTowerDailyProc( sysarg )
	--print("OnSkyTowerDailyProc, ...")
	SetSkyTowerTodayFloorIdx( sysarg, 0 )
	SendSkyTowerData( sysarg )
	if IsInSkyTowerFuben( sysarg ) then 		--跨0点时，如果正在副本中，则踢出副本
		Actor.exitFuben( sysarg )
	end
end



function SkyTowerOp( sysarg, opType )
	print("SkyTowerOp, opType="..opType)
	if opType == 1 then
		SendSkyTowerData( sysarg )
	elseif opType == 2 then 					--从外部挑战下一层
		ChanllegeSkyTower( sysarg, true )
	elseif opType == 3 then
		SweepSkyTower( sysarg )
	elseif opType == 4 then
		ReChanllegeSkyTower( sysarg )			
	elseif opType == 5 then 					--从内部挑战下一层
		ChanllegeSkyTower( sysarg, false )
	end
end

function IsInSkyTowerFuben( sysarg )
	return Actor.getFubenId( sysarg ) == SkyTowerCfg.fubenId
end

function IsInSkyTowerFubenById( fubenId )
	return fubenId == SkyTowerCfg.fubenId
end


--[[
挑战当日层次
可以从通天塔外开始调挑战，也可以再塔内进行挑战
fromOutside: true-从外部进入，false-从内部进入（点按钮或被拉入）
]]
function ChanllegeSkyTower( sysarg, fromOutside )
	--todayFloorIdx：今日应该从第todayFloorIdx+1开始打，而不是直接打最高层
	--[[
	if fromOutside then
		print("ChanllegeSkyTower, fromOutside = true")
	else
		print("ChanllegeSkyTower, fromOutside = false")
	end
	]]
	
	local todayFloorIdx, topFloorIdx, topfightTime = GetSkyTowerData( sysarg )			--玩家通天塔的信息
	if todayFloorIdx >= #SkyTowerCfg.floors then 	--通关了
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.SkyTower008, ttFlyTip)
		return
	end

	local fightStage	= GetSkyTowerStageByFloorIdx( todayFloorIdx+1 )					--要战斗的楼层的stage
	local nextTopStage 	= GetSkyTowerStageByFloorIdx( topFloorIdx + 1 )					--可挑战下一层（最高层的下一层）的stage
	--print("ChanllegeSkyTower, fightStage="..fightStage..", nextTopStage="..nextTopStage)
	if fightStage < nextTopStage then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.SkyTower003, ttFlyTip)
		return
	end

	local nextFloorIdx = todayFloorIdx + 1
	--print("ChanllegeSkyTower, nextFloorIdx="..nextFloorIdx)
	local nextFloorCfg = SkyTowerCfg.floors[nextFloorIdx]
	if not nextFloorCfg then
		return
	end

	local fubenId = Actor.getFubenId( sysarg )
	--print("ChanllegeSkyTower, fubenId="..fubenId)
	if fromOutside then 		--从外部进入下一层
		if not CommonEnterScence( sysarg, SkyTowerCfg, 0, "", nil, nil ) then
			return
		end
	else 						--从内部进入下一层
		if fubenId > 0 and fubenId == SkyTowerCfg.fubenId then		--已经在通天塔副本中
			local skyTowerFubenPtr 	= Actor.getFubenPrt(sysarg)
			local floorIdx 	= Fuben.getFbValue( skyTowerFubenPtr, SKYTOWER_FUBEN_FLOORIDX ) 		--正在打的楼层
			if floorIdx == nextFloorIdx then 			--当前正在打楼层就是目标楼层(防止重复刷怪)
				print("ChanllegeSkyTower, already in， floorIdx="..floorIdx..", nextFloorIdx="..nextFloorIdx)
				return
			end
		else
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.SkyTower010, ttFlyTip)
			return
		end		
	end

	--要清除掉法师放的火
	local hScene = Actor.getSceneHandle( sysarg )
	Fuben.clearSceneFire( hScene )

	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	Fuben.unregScriptCallback(fubenPtr, "AutoSkyTowerChanllegeNext")
	CloseBoardPanel( sysarg )

	local fubenHandle 	= Fuben.getFubenHandle( fubenPtr )
	FreshCommonMonsters( nextFloorCfg.monsters, fubenHandle, true )
	Fuben.setFubenTime( fubenHandle, SkyTowerCfg.floorTime )				--重置副本时间

	local actorId = Actor.getActorId( sysarg )
	local now = System.getCurrMiniTime()
	Fuben.setOwnerId( fubenPtr, actorId )
	Fuben.setFbValue( fubenPtr, SKYTOWER_FUBEN_FLOORIDX, nextFloorIdx )		--设置目标楼层为下一层
	Fuben.setFbValue( fubenPtr, SKYTOWER_FUBEN_BOSS_BEGINTIME, now )
	Fuben.setFbValue( fubenPtr, SKYTOWER_FUBEN_CHANLLEGE_TYPE, 1 )			--正常挑战
	InitSkyTowerPanel( sysarg )

	Actor.triggerQuestEvent(sysarg, 45, 10, 1) 					--任务事件-参加通天塔1次
end


function InitSkyTowerPanel( sysarg )
	local fubenPtr = Actor.getFubenPrt( sysarg )
	if not fubenPtr then
		return
	end
	local floorIdx 	= Fuben.getFbValue( fubenPtr, SKYTOWER_FUBEN_FLOORIDX ) 		--正在打得楼层
	local floorCfg = SkyTowerCfg.floors[floorIdx]
	if not floorCfg then
		return
	end

	local chanllegeType = Fuben.getFbValue( fubenPtr, SKYTOWER_FUBEN_CHANLLEGE_TYPE )
	local todayFloorIdx, topFloorIdx, topfightTime = GetSkyTowerData( sysarg )
	local floorAwards	= nil
	if chanllegeType == 1 then					--正常挑战才有奖励
		if floorIdx > topFloorIdx then 				--冲击新高度
			floorAwards = floorCfg.awards[2] 		--首杀奖励（不拿通关奖励）
		else
			floorAwards = floorCfg.awards[1] 		--通关奖励（不拿首杀奖励）
		end
	end

	local activityId 	= enRightPanelActivity_SkyTower
	local title			= string.format(Lang.ScriptTips.SkyTowerName, floorIdx )
	local contStyle		= 1


	local fubenCfg 		= {}
	fubenCfg.sceneId 	= SkyTowerCfg.sceneId
	fubenCfg.fubenId 	= SkyTowerCfg.fubenId
	fubenCfg.floorIdx 	= floorIdx
	fubenCfg.enterPos 	= SkyTowerCfg.enterPos
	fubenCfg.monsters 	= floorCfg.monsters
	fubenCfg.awards 	= floorAwards

	local panelParams = {}
	panelParams.contents = {}
	if chanllegeType == 1 then
		if floorIdx > topFloorIdx then
			panelParams.contents[1] = Lang.ScriptTips.panelCont41			--挑战（之前已经通关过）
		else
			panelParams.contents[1] = Lang.ScriptTips.panelCont38			--首战（之前尚未通关过）
		end
	else
		panelParams.contents[1] = Lang.ScriptTips.panelCont39				--刷榜（再次打最高层，目的是刷榜）
		panelParams.contents[2] = string.format( Lang.ScriptTips.panelCont40, topfightTime ) 
	end
	panelParams.showStyle = 3  			--退出按钮显示成门状
	OpenCommonStylePanel( sysarg, 1, activityId, 0, title, fubenCfg, contStyle, {}, panelParams )
end


function GetSkyTowerStageByFloorIdx( floorIdx )
	return math.floor( (floorIdx-1)/SKYTOWER_STAGE_FLOOR_NUM )+1
end

--获取要挑战楼层（最高通过楼层的下一层）的stage和前一个stage
function GetSkyTowerNextTopAndPrivStage( sysarg )
	local todayFloorIdx, topFloorIdx, topfightTime = GetSkyTowerData( sysarg )			--玩家已经通关的信息
	if topFloorIdx <= 0 then
		return 0, -1
	end
	local nextTopStage 	= math.floor( (topFloorIdx)/SKYTOWER_STAGE_FLOOR_NUM ) + 1 		--可以挑战的最高楼层
	local privStage 	= nextTopStage - 1
	return nextTopStage, privStage
end


--[[
0-4、5-9、10-14、15-20
扫荡,从1层到第N-1阶的最高层
]]
function SweepSkyTower( sysarg )
	local nextTopStage, privStage = GetSkyTowerNextTopAndPrivStage( sysarg )
	--print("SweepSkyTower, nextTopStage="..nextTopStage..", privStage="..privStage)
	if privStage <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.SkyTower002, ttFlyTip)
		return
	end

	local todayFloorIdx, topFloorIdx, topfightTime = GetSkyTowerData( sysarg )			--玩家通天塔的信息
	if todayFloorIdx > 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.SkyTower004, ttFlyTip)
		return
	end

	local sweepFloorIdx	= privStage*SKYTOWER_STAGE_FLOOR_NUM
	--print("SweepSkyTower, sweepFloorIdx="..sweepFloorIdx)
	local sweepFloorCfg = SkyTowerCfg.floors[sweepFloorIdx]
	if not sweepFloorCfg then
		return
	end

	local sweepAwards = sweepFloorCfg.awards[3]				--扫荡奖励（实际上已经是包括从第1层到该层的扫荡奖励，策划配置）
	if not sweepAwards then
		return
	end
	SetSkyTowerTodayFloorIdx( sysarg, sweepFloorIdx )		--设置扫荡到了这一层
	GiveCommonAward(sysarg, sweepAwards, GameLog.clSkyTowerSweepAward, Lang.ScriptTips.SkyTowerLog01)
	SendSkyTowerData( sysarg )
end

--[[
再次挑战某一层
必须从外面挑战
]]
function ReChanllegeSkyTower( sysarg )
	local todayFloorIdx, topFloorIdx, topfightTime = GetSkyTowerData( sysarg )
	local topFloorCfg = SkyTowerCfg.floors[topFloorIdx]
	if not topFloorCfg then
		return
	end

	local floorCfg = {}
	floorCfg.sceneId 			= SkyTowerCfg.sceneId
	floorCfg.fubenId 			= SkyTowerCfg.fubenId
	floorCfg.enterPos 			= SkyTowerCfg.enterPos
	floorCfg.enterConsume		= topFloorCfg.reChanllegeFee
	floorCfg.monsters 			= topFloorCfg.monsters
	floorCfg.autoFreshMonster	= true		--进入副本自动刷怪
	floorCfg.reloginType		= SkyTowerCfg.reloginType
	if CommonEnterScence( sysarg, floorCfg, GameLog.clSkyTowerReChanllegeFee, Lang.ScriptTips.SkyTowerLog02, nil, nil ) then
		local actorId = Actor.getActorId( sysarg )
		local now = System.getCurrMiniTime()
		local fubenPtr 	= Actor.getFubenPrt(sysarg)
		Fuben.setOwnerId( fubenPtr, actorId )
		Fuben.setFbValue( fubenPtr, SKYTOWER_FUBEN_FLOORIDX, topFloorIdx )		--当前就是历史最高层
		Fuben.setFbValue( fubenPtr, SKYTOWER_FUBEN_BOSS_BEGINTIME, now )
		Fuben.setFbValue( fubenPtr, SKYTOWER_FUBEN_CHANLLEGE_TYPE, 2 )			--再次挑战

		InitSkyTowerPanel( sysarg )
		--print("ReChanllegeSkyTower, topFloorIdx="..topFloorIdx)
	end
end
	

--[[
--sysarg:归属玩家
--lastHitKiller：最后一击玩家
注意：怪物死亡时，玩家可能不在线，要确保副本进度能够继续进行
]]
function OnSkyTowerBossDie( monster, sysarg, monId, lastHitKiller )
	local fubenPtr 		= Actor.getFubenPrt(monster)
	local passFloorIdx 	= Fuben.getFbValue( fubenPtr, SKYTOWER_FUBEN_FLOORIDX ) 		--正在打的楼层
	local todayFloorIdx, topFloorIdx, topfightTime = GetSkyTowerData( sysarg )			--玩家已经通关的信息
	--print("OnSkyTowerBossDie, monId="..monId..", passFloorIdx="..passFloorIdx..", topFloorIdx="..topFloorIdx)
	local passFloorCfg = SkyTowerCfg.floors[passFloorIdx]
	if not passFloorCfg then
		return
	end

	local chanllegeType = Fuben.getFbValue( fubenPtr, SKYTOWER_FUBEN_CHANLLEGE_TYPE )
	--print("OnSkyTowerBossDie, chanllegeType="..chanllegeType)

	local fubenHandle = Actor.getFubenHandle( monster )
	local allBoss, killBoss, allMon, killMon = GetCommonBossMonsterNum6( fubenHandle, passFloorCfg.monsters )
	--print("OnSkyTowerBossDie, allBoss="..allBoss..", killBoss="..killBoss..", allMon="..allMon..", killMon="..killMon)
	if killBoss + killMon >= allBoss + allMon then 		--怪物击杀完毕
		--print("OnSkyTowerBossDie, kill all, passFloorIdx="..passFloorIdx..", topFloorIdx="..topFloorIdx)
		local getAwards = nil
		if chanllegeType == 1 and passFloorIdx <= topFloorIdx then		--正常挑战，并且非首杀
			local passAwards = passFloorCfg.awards[1]			--通关奖励
			GiveCommonAward(sysarg, passAwards, GameLog.clSkyTowerPassAward, Lang.ScriptTips.SkyTowerLog03)
			getAwards = passAwards
		end

		if passFloorIdx >= topFloorIdx then		--首杀或继续挑战，走这里
			local beginTime = Fuben.getFbValue( fubenPtr, SKYTOWER_FUBEN_BOSS_BEGINTIME )
			local now 		= System.getCurrMiniTime()
			topfightTime = now - beginTime
			if topfightTime < 0 then
				topfightTime = 0
			end

			if passFloorIdx > topFloorIdx then 							--新高度
				local firstPassAwards = passFloorCfg.awards[2]			--首次通关奖励
				--GiveCommonAward(sysarg, firstPassAwards, GameLog.clSkyTowerPassAward, Lang.ScriptTips.SkyTowerLog04)
				local mail 	= {}
				mail.title 	= Lang.ScriptTips.SkyTowerMailTitle
				mail.content= string.format( Lang.ScriptTips.SkyTowerMailCont, passFloorIdx )
				local logId = GameLog.clSkyTowerFirstKillAward
				local logStr= Lang.ScriptTips.SkyTowerLog04
				getAwards = GiveCommonAwardsBagOrMail( sysarg, firstPassAwards, 1, mail, logId, logStr )

				topFloorIdx = passFloorIdx
				SetSkyTowerTopFloorAndTime( sysarg, topFloorIdx, topfightTime )			--更新排行榜数据

				Actor.triggerAchieveEvent(sysarg, aAchieveEventFinishTarget, 4, topFloorIdx)	--通天塔新的最高层
			elseif passFloorIdx == topFloorIdx then 									--再次打或刷榜
				local oldTopFloorIdx, oldTopFloorTime = GetSkyTowerTopFloorAndTime( sysarg )
				if oldTopFloorIdx == passFloorIdx and oldTopFloorTime <= 0 then
					SetSkyTowerTopFloorAndTime( sysarg, topFloorIdx, topfightTime )			--更新排行榜数据
					local msg = string.format( Lang.ScriptTips.SkyTower006, topFloorIdx, topfightTime )
					Actor.sendTipmsg(sysarg, msg, ttFlyTip+ttChatWindow)
				elseif oldTopFloorIdx == passFloorIdx and oldTopFloorTime > 0 and topfightTime < oldTopFloorTime then
					SetSkyTowerTopFloorAndTime( sysarg, topFloorIdx, topfightTime )			--更新排行榜数据
					local msg = string.format( Lang.ScriptTips.SkyTower006, topFloorIdx, topfightTime )
					Actor.sendTipmsg(sysarg, msg, ttFlyTip+ttChatWindow)
				else
					local msg = string.format( Lang.ScriptTips.SkyTower007, topFloorIdx, topfightTime )
					Actor.sendTipmsg(sysarg, msg, ttFlyTip+ttChatWindow)
				end
			end
			--print("OnSkyTowerBossDie, beginTime="..beginTime..", now="..now..", topfightTime="..topfightTime)
		end

		--print("OnSkyTowerBossDie, passFloorIdx="..passFloorIdx..", topFloorIdx="..topFloorIdx..", topfightTime="..topfightTime)
		SetSkyTowerData( sysarg, passFloorIdx, topFloorIdx, topfightTime )
		SendSkyTowerData( sysarg )

		if chanllegeType == 2 then				--刷榜，打完踢出
			--OnSkyTowerOver( sysarg, fubenPtr, 1, getAwards )				--刷榜成功不弹出面板
			Actor.exitFuben( sysarg )
		else
			OnSkyTowerOver( sysarg, fubenPtr, 1, getAwards )				--客户端要注意最后一关通关，没有"下一层"按钮的。
			if passFloorIdx < #SkyTowerCfg.floors then 	--尚未通关
				local boardStr = Lang.ScriptTips.SkyTower005				--注意：字符串中要带有一个 %d
				OpenBoardPanel( sysarg, enBoardPanelStyle_CountDown1, boardStr, SkyTowerCfg.nextWaitCD )

				Fuben.unregScriptCallback(fubenPtr, "AutoSkyTowerChanllegeNext")
				local interval = SkyTowerCfg.nextWaitCD * 1000
				Fuben.regScriptTimer2S(fubenPtr, 0, interval, interval, 1, "AutoSkyTowerChanllegeNext")			--一段时间后执行
			else
				Actor.exitFuben( sysarg ) 			--通关踢出副本
				Actor.sendTipmsg(sysarg, Lang.ScriptTips.SkyTower009, ttFlyTip+ttChatWindow)
			end 
		end
	end
	
	local activitySts = nil
	local contStyle 	= 1

	local panelParams = {}
	panelParams.contents = {}
	if chanllegeType == 1 then
		if passFloorIdx > topFloorIdx then
			panelParams.contents[1] = Lang.ScriptTips.panelCont41			--挑战（之前已经通关过）
		else
			panelParams.contents[1] = Lang.ScriptTips.panelCont38			--首战（之前尚未通关过）
		end
	else
		panelParams.contents[1] = Lang.ScriptTips.panelCont39				--刷榜（再次打最高层，目的是刷榜）
		panelParams.contents[2] = string.format( Lang.ScriptTips.panelCont40, topfightTime ) 
	end
	ChangeCommonStylePanel( sysarg, activitySts, contStyle, passFloorCfg, {}, {}, panelParams )
end

--[[
自动挑战下一层
]]
function AutoSkyTowerChanllegeNext( fubenPtr )
	local actorId 	= Fuben.getOwnerId( fubenPtr )
	local actorPtr 	= Actor.getActorById( actorId )
	if actorPtr then
		ChanllegeSkyTower( actorPtr, false )		--被动进入
		Actor.closeDialogs( actorPtr, enLinkSkyTowerOKWnd )
	end
end

--[[
设置当天可以打得楼层
]]
function SetSkyTowerTodayFloorIdx( sysarg, todayFloorIdx )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.skyTowerData then
		sVar.skyTowerData = {}
	end
	sVar.skyTowerData[1] = todayFloorIdx
end

function GetSkyTowerData( sysarg )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.skyTowerData then
		sVar.skyTowerData = {}
	end
	local todayFloorIdx = sVar.skyTowerData[1] or 0 		--当天最大通关层数
	local topFloorIdx 	= sVar.skyTowerData[2] or 0			--历史最大通关层数
	local topFightTime	= sVar.skyTowerData[3] or 0			--历史最大通关层数的时间
	return todayFloorIdx, topFloorIdx, topFightTime
end

function SetSkyTowerData( sysarg, todayFloorIdx, topFloorIdx, topFightTime )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.skyTowerData then
		sVar.skyTowerData = {}
	end
	sVar.skyTowerData[1] = todayFloorIdx
	sVar.skyTowerData[2] = topFloorIdx
	sVar.skyTowerData[3] = topFightTime
end

function SendSkyTowerData( sysarg )
	local npack = DataPack.allocPacket(sysarg, enBossSystemID, sSendSkyTowerData)
	if npack then
		local todayFloorIdx, topFloorIdx, topFightTime = GetSkyTowerData( sysarg )

		DataPack.writeShort(npack, todayFloorIdx)
		DataPack.writeShort(npack, topFloorIdx)
		DataPack.writeUInt(npack, topFightTime)
		DataPack.flush(npack)

		--print("SendSkyTowerData, todayFloorIdx="..todayFloorIdx..", topFloorIdx="..topFloorIdx..", topFightTime="..topFightTime)
	end
end

--[[
此时玩家可能副本中或者副本外
]]
function OnSkyTowerOver( sysarg, fubenPtr, sts, getAwards )
	--print( "OnSkyTowerOver, sts="..sts )
	local floorIdx 	= Fuben.getFbValue( fubenPtr, SKYTOWER_FUBEN_FLOORIDX ) 		--正在打得楼层
	local todayFloorIdx, topFloorIdx, topfightTime = GetSkyTowerData( sysarg )		--玩家已经通关的信息
	--print("OnSkyTowerOver, todayFloorIdx="..todayFloorIdx..", floorIdx="..floorIdx)
	if sts == 0 and todayFloorIdx >= floorIdx then 		--该层实际上今天通过了
		return
	end

	local chanllegeType = Fuben.getFbValue( fubenPtr, SKYTOWER_FUBEN_CHANLLEGE_TYPE )
	if chanllegeType == 2 and sts == 1 then 		--刷榜成功不弹出面板
		return
	end

	local panelInfo 	= {}
	panelInfo.sts 		= sts
	panelInfo.awards 	= getAwards
	panelInfo.param1	= chanllegeType
	PassActivityRightPanel( sysarg, 1, enRightPanelActivity_SkyTower, panelInfo )
end


function GetSkyTowerTopFloorAndTime( sysarg )
	local actorId = Actor.getActorId( sysarg )
	local topFloorIdx 	= GetCommonRankColumnValueById( actorId, SkyTowerRank, 3 ) or 0
	local topFloorTime 	= GetCommonRankColumnValueById( actorId, SkyTowerRank, 4 ) or 0
	return topFloorIdx, topFloorTime
end
--[[
newFightTime: 应该不超过1000秒
]]
function SetSkyTowerTopFloorAndTime( sysarg, newFloorIdx, newFightTime )
	local lastTime = 1000-newFightTime
	if lastTime < 0 then
		lastTime = 0
	end
	local newScore =  newFloorIdx*1000+lastTime				--后3位是剩余时间，剩余时间（耗时越短）越长的排名考靠前
	--print( "SetSkyTowerTopFloorAndTime, newScore="..newScore..", newFloorIdx="..newFloorIdx..", newFightTime="..newFightTime)
	local actorId 	= Actor.getActorId( sysarg )
	local actorName = Actor.getName( sysarg )
	local job 		= Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION)
	SetCommonRankValueById( actorId, SkyTowerRank, newScore )
	SetCommonRankColumnValueById( actorId, SkyTowerRank, 0, 3, newFloorIdx)
	SetCommonRankColumnValueById( actorId, SkyTowerRank, 0, 4, newFightTime)
	SetCommonRankActorData( SkyTowerRank, actorId, actorName, job )
end


ResisterSkyTowerProc()

InitSkyTowerRank()

